package Spil;

import java.awt.Color;

import desktop_codebehind.Car;
import desktop_resources.GUI;

public class Spil {

	private int numberOfPlayers;
	private int playerNumber;
	private boolean gameOn = true;
	private int endCount;
	private Color[] color = {Color.RED, Color.blue, Color.green, Color.yellow, Color.black, Color.white};
	private int i = 0;
	private IntroGUI intro = new IntroGUI();
	private FieldGUI field = new FieldGUI();
	private Cup cup = new Cup();
	private Dice dice = new Dice();
	private GameBoard board = new GameBoard();
	
	public static void main(String[] args) {
		new Spil().start();
	}

	public void start() {
		while(intro.getWait() == false) {
			intro.setVisible(true);
			//Venter for spiller til at vælge antal
		}

		intro.dispose();
		
		while(field.getWait() == false) {
			field.setVisible(true);
		}

		field.dispose();
		
		if (field.getAnswer() == Txt.Yes)
			board.randomizer();
		
		numberOfPlayers = intro.getNumber();
		
		board.create();

		Player[] playerList = new Player[numberOfPlayers + 1]; //Spillertabllen oprettes

		GUI.showMessage(Txt.Intro1 + numberOfPlayers + Txt.Intro2);

		for (playerNumber = 1; playerNumber < numberOfPlayers+1; playerNumber++)
		{
			playerList[playerNumber] = new Player(GUI.getUserString(Txt.NewPlayer + playerNumber + ":"), board.getStartNumber()); //Tilføjelse af spiller objekt i spillertabellen
			Car car = new Car.Builder()
			.primaryColor(color[i])
			.typeRacecar()
			.build();
			GUI.addPlayer(playerList[playerNumber].toString(), playerList[playerNumber].bank.getBalance(), car);
			GUI.setCar(playerList[playerNumber].getFieldNumber(), playerList[playerNumber].toString());
			i++;
		}

		while (gameOn)
		{
			for (playerNumber = 1; playerNumber < numberOfPlayers + 1; playerNumber++)
			{
				if (playerList[playerNumber] != null) {
					GUI.getUserButtonPressed(Txt.Tourn1 + playerList[playerNumber].toString() + Txt.Tourn2, Txt.Roll);
					cup.rollDice(dice.roll(), dice.roll());
					GUI.removeCar(playerList[playerNumber].getFieldNumber(), playerList[playerNumber].toString());
					playerList[playerNumber].addFieldNumber(cup.getSum());

					GUI.setCar(playerList[playerNumber].getFieldNumber(), playerList[playerNumber].toString());
					board.getField(playerList[playerNumber].getFieldNumber() - 1).landOnField(playerList[playerNumber]);

					if (playerList[playerNumber].bank.getLose()) {
						GUI.removeCar(playerList[playerNumber].getFieldNumber(), playerList[playerNumber].toString());
						
						for (i = 0; i < 22; i++) {
							board.getField(i).removeOwner(playerList[playerNumber], i);
						}
						
						playerList[playerNumber] = null;
					}
				}
			}
			
			endCount = 0;
			
			for (playerNumber = 1; playerNumber < numberOfPlayers + 1; playerNumber++) {
				if (playerList[playerNumber] == null) {
					endCount++;
				}
			}
			
			if (endCount == numberOfPlayers - 1) { //En vinder
				gameOn = false;
				for (playerNumber = 1; playerNumber < numberOfPlayers + 1; playerNumber++) {
					if (playerList[playerNumber] != null) {
						GUI.showMessage(playerList[playerNumber].toString() + Txt.Win + playerList[playerNumber].toString() + Txt.Win1);
					}
				}
			}
			else if (endCount == numberOfPlayers) { //Hvis alle taber
				gameOn = false;
				GUI.showMessage(Txt.AllLoose);
			}
			
		}		
		
		GUI.close();
	}
}
